/* eslint-disable @typescript-eslint/naming-convention */
import React, { useEffect, useRef, useCallback } from 'react';
import isEqual from 'lodash.isequal';

/**
 * 把 value 的枚举转化为数组
 * @param valueEnum
 */
export const parsingValueEnumToArray = (valueEnum = new Map()) => {
    const enumArray = [];
    valueEnum.forEach((_, key) => {
        if (!valueEnum.has(key) && !valueEnum.has(`${key}`)) {
            return;
        }
        const value = (valueEnum.get(key) || valueEnum.get(`${key}`));
        if (!value) {
            return;
        }
        if (typeof value === 'object' && value?.text) {
            enumArray.push({
                text: value?.text,
                value: key,
            });
            return;
        }
        enumArray.push({
            text: (value || ''),
            value: key,
        });
    });
    return enumArray;
};
/**
 * 检查值是否存在
 * 为了 避开 0 和 false
 * @param value
 */
export const checkUndefinedOrNull = (value) => value !== undefined && value !== null;
function deepCompareEquals(a, b) {
    return isEqual(a, b);
}
function useDeepCompareMemoize(value) {
    const ref = useRef();
    // it can be done by using useMemo as well
    // but useRef is rather cleaner and easier
    if (!deepCompareEquals(value, ref.current)) {
        ref.current = value;
    }
    return ref.current;
}
export function useDeepCompareEffect(effect, dependencies) {
    useEffect(effect, useDeepCompareMemoize(dependencies));
}
export function getProgressStatus(text) {
    if (typeof text !== 'number') {
        return 'exception';
    }
    if (text === 100) {
        return 'success';
    }
    if (text < 100) {
        return 'active';
    }
    // magic
    if (text < 0) {
        return 'exception';
    }
    return 'normal';
}
/**
 *  根据 key 和 dataIndex 生成唯一 id
 * @param key 用户设置的 key
 * @param dataIndex 在对象中的数据
 * @param index 序列号，理论上唯一
 */
export const genColumnKey = (key, dataIndex, index) => {
    if (key) {
        return key;
    }
    if (!key && dataIndex) {
        if (Array.isArray(dataIndex)) {
            return dataIndex.join('-');
        }
        return dataIndex;
    }
    return `${index}`;
};
export default function get(entity, path) {
    let tempPath = [''];
    if (typeof path === 'string') {
        if (path.includes('.')) {
            tempPath = path.split('.');
        }
        else {
            tempPath = [path];
        }
    }
    if (Array.isArray(path)) {
        tempPath = path;
    }
    let current = entity;
    for (let i = 0; i < tempPath.length; i += 1) {
        if (current === null || current === undefined) {
            return undefined;
        }
        current = current[tempPath[i]];
    }
    return current;
}
export const usePrevious = (state) => {
    const ref = useRef();
    useEffect(() => {
        ref.current = state;
    });
    return ref.current;
};
const useUpdateEffect = (effect, deps) => {
    const isMounted = useRef(false);
    useEffect(() => {
        if (!isMounted.current) {
            isMounted.current = true;
        }
        else {
            return effect();
        }
        return () => undefined;
    }, deps);
};
export function useDebounceFn(fn, deps, wait) {
    // eslint-disable-next-line no-underscore-dangle
    const _deps = (Array.isArray(deps) ? deps : []);
    // eslint-disable-next-line no-underscore-dangle
    const _wait = typeof deps === 'number' ? deps : wait || 0;
    const timer = useRef();
    const fnRef = useRef(fn);
    fnRef.current = fn;
    const cancel = useCallback(() => {
        if (timer.current) {
            clearTimeout(timer.current);
        }
    }, []);
    const run = useCallback((...args) => {
        cancel();
        timer.current = setTimeout(() => {
            fnRef.current(...args);
        }, _wait);
    }, [_wait, cancel]);
    useUpdateEffect(() => {
        run();
        return cancel;
    }, [..._deps, run]);
    useEffect(() => cancel, []);
    return {
        run,
        cancel,
    };
}
export const getLang = () => {
    const isNavigatorLanguageValid = typeof navigator !== 'undefined' && typeof navigator.language === 'string';
    const browserLang = isNavigatorLanguageValid
        ? navigator.language.split('-').join('{{BaseSeparator}}')
        : '';
    const lang = typeof localStorage !== 'undefined' ? window.localStorage.getItem('umi_locale') : '';
    return lang || browserLang || '';
};
/**
 * 删除对象中所有的空值
 * @param obj
 */
export const removeObjectNull = (obj) => {
    const newObj = {};
    Object.keys(obj).forEach((key) => {
        if (obj[key]) {
            newObj[key] = obj[key];
        }
    });
    return newObj;
};
/**
 * 获取类型的 type
 * @param obj
 */
function getType(obj) {
    // @ts-ignore
    const type = Object.prototype.toString
        .call(obj)
        .match(/^\[object (.*)\]$/)[1]
        .toLowerCase();
    if (type === 'string' && typeof obj === 'object')
        return 'object'; // Let "new String('')" return 'object'
    if (obj === null)
        return 'null'; // PhantomJS has type "DOMWindow" for null
    if (obj === undefined)
        return 'undefined'; // PhantomJS has type "DOMWindow" for undefined
    return type;
}
export const ObjToMap = (value) => {
    if (!value) {
        return value;
    }
    if (getType(value) === 'map') {
        return value;
    }
    return new Map(Object.entries(value));
};
/**
 * 减少 width，支持 string 和 number
 */
export const reduceWidth = (width) => {
    if (width === undefined) {
        return width;
    }
    if (typeof width === 'string') {
        if (!width.includes('calc')) {
            return `calc(100% - ${width})`;
        }
        return width;
    }
    if (typeof width === 'number') {
        return width - 32;
    }
    return width;
};
